package edu.uoc.tds.pac4.client.ss3;

import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.Date;

import javax.swing.JButton;

import edu.uoc.tds.pac4.beans.Participant;
import edu.uoc.tds.pac4.beans.TipusEvent;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;
import javax.swing.JCheckBox;

public class JPanelFiltreEventsAssistent extends JPanel {

	private static final long serialVersionUID = 1L;
	protected static final int defaultHeight = 20;
	private JLabel jLabelAssistent = null;  
	private JLabel jLabelTipusEvent = null;
	private JLabel jLabelDataInici = null; 
	private JLabel jLabelDataFi = null;  
	private JLabel jLabelActiu = null;
	private JComboBox jComboBoxAssistent = null;
	private JComboBox jComboBoxTipusEvent = null;
	private JTextField jTextFieldDataInici = null;
	private JTextField jTextFieldDataFi = null;  
	private JCheckBox jCheckBoxActiu = null; 
	private JButton jButtonFiltre = null; 
	private InterfaceFiltreEstadistiques pantalla = null;
	
	
	/**
	 * This is the default constructor
	 */
	public JPanelFiltreEventsAssistent(InterfaceFiltreEstadistiques pantalla) {
		this.pantalla = pantalla;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gbcButtonFiltre = new GridBagConstraints();
		gbcButtonFiltre.gridx = 5;
		gbcButtonFiltre.gridy = 1;
		gbcButtonFiltre.insets = new Insets(5, 10, 5, 10);

		// Assistent
		GridBagConstraints gbcLabelAssistent = new GridBagConstraints();
		gbcLabelAssistent.gridx = 0;
		gbcLabelAssistent.gridy = 0;
		gbcLabelAssistent.anchor = GridBagConstraints.LINE_END;
		jLabelAssistent = new JLabel();
		jLabelAssistent.setText(TDSLanguageUtils
				.getMessage("Client.PantallaEventsAssistent.AssistentNom"));
		GridBagConstraints gbcComboBoxAssistent = new GridBagConstraints();
		gbcComboBoxAssistent.gridx = 1;
		gbcComboBoxAssistent.gridy = 0;
		gbcComboBoxAssistent.insets = new Insets(5, 10, 5, 10);
		// TipusEvent
		GridBagConstraints gbcLblTipusEvent = new GridBagConstraints();
		gbcLblTipusEvent.gridx = 0;
		gbcLblTipusEvent.gridy = 1;
		gbcLblTipusEvent.anchor = GridBagConstraints.LINE_END;
		jLabelTipusEvent = new JLabel();
		jLabelTipusEvent.setText(TDSLanguageUtils
				.getMessage("Client.PantallaEventsAssistent.TipusEvent"));
		GridBagConstraints gbcComboBoxTipusEvent = new GridBagConstraints();
		gbcComboBoxTipusEvent.weightx = 0.7;
		gbcComboBoxTipusEvent.gridx = 1;
		gbcComboBoxTipusEvent.gridy = 1;
		gbcComboBoxTipusEvent.insets = new Insets(5, 10, 5, 10);
		//Dates
		GridBagConstraints gbcTextDataInici = new GridBagConstraints();
		gbcTextDataInici.gridx = 3;
		gbcTextDataInici.gridy = 0;
		gbcTextDataInici.insets = new Insets(5, 10, 5, 10);

		
		GridBagConstraints gbcLabelDataInici = new GridBagConstraints();
		gbcLabelDataInici.gridx = 2;
		gbcLabelDataInici.gridy = 0;
		gbcLabelDataInici.anchor = GridBagConstraints.LINE_END;
		jLabelDataInici = new JLabel();
		jLabelDataInici.setText(TDSLanguageUtils
				.getMessage("Client.PantallaEventsAssistent.DataInici"));

		GridBagConstraints gbcTextDataFi = new GridBagConstraints();
		gbcTextDataFi.gridx = 3;
		gbcTextDataFi.gridy = 1;
		gbcTextDataFi.insets = new Insets(5, 10, 5, 10);
		GridBagConstraints gbcLabelDataFi = new GridBagConstraints();
		gbcLabelDataFi.gridx = 2;
		gbcLabelDataFi.gridy = 1;
		gbcLabelDataFi.anchor = GridBagConstraints.LINE_END;
		jLabelDataFi = new JLabel();
		jLabelDataFi.setText(TDSLanguageUtils
				.getMessage("Client.PantallaEventsAssistent.DataFi"));

		//Actiu
		GridBagConstraints gbcCheckBoxActiu = new GridBagConstraints();
		gbcCheckBoxActiu.gridx = 5;
		gbcCheckBoxActiu.gridy = 0;
		gbcCheckBoxActiu.anchor = GridBagConstraints.LINE_START;
		gbcCheckBoxActiu.insets = new Insets(5, 10, 5, 10);
		GridBagConstraints gbcLabelActiu = new GridBagConstraints();
		gbcLabelActiu.gridx = 4;
		gbcLabelActiu.gridy = 0;
		gbcLabelActiu.anchor = GridBagConstraints.LINE_END;
		jLabelActiu = new JLabel();
		jLabelActiu.setText(TDSLanguageUtils
				.getMessage("Client.PantallaEventsAssistent.Actiu"));

		// JPanel
		this.setLayout(new GridBagLayout());
		this.setName("jPanelFiltre");
		this.add(jLabelAssistent, gbcLabelAssistent);
		this.add(getJcomboBoxAssistent(), gbcComboBoxAssistent);
		this.add(getJButtonFiltre(), gbcButtonFiltre);
		this.add(jLabelTipusEvent, gbcLblTipusEvent);
		this.add(getJComboBoxTipusEvent(), gbcComboBoxTipusEvent);
		this.add(jLabelDataInici,gbcLabelDataInici);
		this.add(getJTextFieldDataInici(),gbcTextDataInici);
		this.add(jLabelDataFi,gbcLabelDataFi);
		this.add(getJTextFieldDataFi(),gbcTextDataFi);
		this.add(jLabelActiu,gbcLabelActiu);
		this.add(getJCheckBoxActiu(),gbcCheckBoxActiu);
	}



	/**
	 * Retorna el assistent seleccionada
	 * 
	 * @return
	 */
	public Participant getParticipant() {
		Participant currentParticipant = null;
		if (jComboBoxAssistent.getSelectedItem() instanceof Participant)
			currentParticipant = (Participant) jComboBoxAssistent.getSelectedItem();
		else
			currentParticipant = null;

		return currentParticipant;
	}

	/**
	 * Retorna El tipus d'event Seleccionat
	 * 
	 * @return
	 */
	public TipusEvent getTipusEvent() {
		TipusEvent currentTipusEvent = null;
		if (jComboBoxTipusEvent.getSelectedItem() instanceof TipusEvent)
			currentTipusEvent = (TipusEvent) jComboBoxTipusEvent.getSelectedItem();
		else
			currentTipusEvent = null;

		return currentTipusEvent;
	}
	
	/**
	 * Retorna la Data Inici
	 * 
	 * @return
	 */
	public Date getDataInici() {
		SimpleDateFormat formatFecha = new SimpleDateFormat("dd/MM/yyyy"); 
		Date currentDataInici = null;
		if (!jTextFieldDataInici.getText().isEmpty()){
			try {
				currentDataInici = formatFecha.parse(jTextFieldDataInici.getText());
			} catch (ParseException e) {
				new PantallaError("Error_Format_Date", PantallaError.Tipus.ERROR);
			}
		}
		return currentDataInici;
	}
	
	/**
	 * Retorna la Data Fi
	 * 
	 * @return
	 */
	public Date getDataFi() {
		SimpleDateFormat formatFecha = new SimpleDateFormat("dd/MM/yyyy"); 
		Date currentDataFi = null;
		if (!jTextFieldDataFi.getText().isEmpty()){
			try {
				currentDataFi = formatFecha.parse(jTextFieldDataFi.getText());
			} catch (ParseException e) {
				new PantallaError("Error_Format_Date", PantallaError.Tipus.ERROR);
			}
		}

		return currentDataFi;
	}
	
	/**
	 * Retorna Actiu
	 * 
	 * @return
	 */
	public boolean getActiu(){
			return jCheckBoxActiu.isSelected();
	}
	
	/**
	 * Inicialitza el boto Filtrar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonFiltre() {
		if (jButtonFiltre == null) {
			jButtonFiltre = new JButton();
			jButtonFiltre.setText(TDSLanguageUtils
				.getMessage("Client.PantallaEventsAssistent.Filtre"));
			jButtonFiltre.setSize(200, 20);
			jButtonFiltre.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {

					pantalla.updateTaula();
				}
			});
		}
		return jButtonFiltre;
	}

	/**
	 * Inicialitza jComboBoxUniversitat
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJcomboBoxAssistent() {
		if (jComboBoxAssistent == null) {
			jComboBoxAssistent = new JComboBox();
			jComboBoxAssistent.setMinimumSize(new Dimension(200, 20));
			jComboBoxAssistent.setPreferredSize(new Dimension(200, 20));
			jComboBoxAssistent.addItem(TDSLanguageUtils
					.getMessage("Client.PantallaEventsAssistent.Tots"));
			Vector<Participant> participants = pantalla.carregarParticipants();
			Helpers.setItemsCombo(participants, jComboBoxAssistent);
		}
		return jComboBoxAssistent;
	}


	/**
	 * Inicialitza jComboBoxTipusEvent
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxTipusEvent() {
		if (jComboBoxTipusEvent == null) {
			jComboBoxTipusEvent = new JComboBox();
			jComboBoxTipusEvent.setMinimumSize(new Dimension(200, 20));
			jComboBoxTipusEvent.setPreferredSize(new Dimension(200, 20));
			jComboBoxTipusEvent.addItem(TDSLanguageUtils
					.getMessage("Client.PantallaEventsAssistent.Tots"));
			Vector<TipusEvent> TipusEvents = pantalla.carregarTipusEvents();
			Helpers.setItemsCombo(TipusEvents, jComboBoxTipusEvent);
			
		}
		return jComboBoxTipusEvent;
	}

	/**
	 * Inicialitza jTextFieldDataInici
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldDataInici() {
		if (jTextFieldDataInici == null) {
			jTextFieldDataInici = new JTextField();
			jTextFieldDataInici
					.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldDataInici.setPreferredSize(new Dimension(80,
					defaultHeight));
			jTextFieldDataInici.addFocusListener(new java.awt.event.FocusAdapter() {
				public void focusLost(java.awt.event.FocusEvent e) {
					if (!jTextFieldDataInici.getText().isEmpty()){
						validaData(jTextFieldDataInici.getText());
					}
				}
			});
		}
		return jTextFieldDataInici;
	}
	/**
	 * Inicialitza jTextFieldDataFi
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldDataFi() {
		if (jTextFieldDataFi == null) {
			jTextFieldDataFi = new JTextField();
			jTextFieldDataFi
					.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldDataFi.setPreferredSize(new Dimension(80,
					defaultHeight));
			jTextFieldDataFi.addFocusListener(new java.awt.event.FocusAdapter() {
				public void focusLost(java.awt.event.FocusEvent e) {
					if (!jTextFieldDataFi.getText().isEmpty()){
						validaData(jTextFieldDataFi.getText());
					}
				}
			});
		}
		return jTextFieldDataFi;
	}

	/**
	 * Inicialitza jCheckBoxActiu	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxActiu() {
		if (jCheckBoxActiu == null) {
			jCheckBoxActiu = new JCheckBox();
		}
		return jCheckBoxActiu;
	}

	private void validaData(String data){
	      SimpleDateFormat formatData = new SimpleDateFormat("dd/MM/yyyy");
	      formatData.setLenient(false);
	      try {
			formatData.parse(data);
		} catch (ParseException e) {
			new PantallaError("Error_Format_Date", PantallaError.Tipus.ERROR);
		}
	}
		  

}

